import os
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from appengine_utilities import sessions
from appengine_utilities import flash
from model.authentication import *

class Register(webapp.RequestHandler):
    def get(self, flash=None):
        path=os.path.join(os.path.dirname(__file__),"../views/register.html")
        # self.flash = flash.Flash()
        #template_values = {"message":message,"replies":replies,"topic":topic,"group":group}
        self.response.out.write(template.render(path,{"flash":flash}))
   
    def post(self):
        self.flash = flash.Flash()
        self.flash.msg = list()
        if self.request.get("pword") != self.request.get("vpword"):
            self.flash.msg.append('Your passowrds do not match.')
        query = Authentication.all()
        query.filter("username =", self.request.get("username"))
        result = query.get()
        if not result:
            query = Authentication(
                first_name = self.request.get("fname"),
                last_name = self.request.get("lname"),
                username = self.request.get("username"),
                password = self.request.get("pword")
            )
            query.put()
        else:
            self.flash.msg.append('That username is already used please try a different one.')
        self.get(flash=self.flash)
            